package ${classPath};

import java.util.Date;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.lemna.base.core.persistence.base.service.BaseService;

@Service
public class ${domain}Service extends BaseService<${domain}> {

  @Autowired
  @SuppressWarnings("unused")
  private ${domain}Repository repository;


  public ${domain} find(String name ) {
    Optional<${domain}> optional = repository.findByName(name);
    return optional.orElseThrow(() -> new ${domain}NotExistException(name));
  }

  public ${domain} create(//
  <#list fieldList as field>
  ${field.type} ${field.name}<#if field_has_next>,</#if>//${field.comment}
  </#list>
  ){
  
    Optional<${domain}> optional = repository.findByName(name);
    if (optional.isPresent()) {
      throw new ${domain}AlreadyExistException(name);
    }
  
    ${domain} entity = new ${domain}();
    <#list fieldList as field>
      entity.set${field.name?cap_first}(${field.name});//${field.comment}
    </#list>
    ${domain} entity = new ${domain}(  //
    <#list fieldList as field> 
    ${field.name}<#if field_has_next>,</#if>//${field.comment} 
    </#list>
    );
    return save(entity);
  }
  
 
}
